<?php
include("../service/productService.php");
include("../service/productDetailService.php");
include("../service/imageService.php");

$proID = $_GET['id'];
if (!isset($proID)) {
    exit;
}

$product = getProductByID($proID);
$statusArray = array(1 => 'Còn Hàng', 2 => 'Hàng Mới Về', 3 => 'Hết Hàng', 4 => 'Hàng Sắp Về', 5 => 'Sale off', 6 => 'Ẩn');
$imagePath = "../img/product/";
$images = getImagesByProID($proID);

$image1 = mysql_fetch_array($images);
$image2 = mysql_fetch_array($images);
$image3 = mysql_fetch_array($images);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title><?php echo($product['PRO_NAME']); ?></title>
        <link href="../css/main.css" type="text/css" rel="stylesheet" />
        <script src="../js/jquery-1.6.4.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function changeImage(self) {
                var imgs = self.getElementsByTagName('img');
                var img = imgs[0];
                document.getElementById('largeImage').src = img.src;
            }
        </script>
    </head>

    <body>
        <div class="wrapper"><?php include("header.php"); ?>
            <div class="main-body"><?php include("left_menu.php"); ?>
                <div class="main-body-right">
                    <div class="paging"></div>
                    <div class="middle-body">
                        <div class="left"><img id="largeImage"
                                               src="<?php echo($imagePath . $image1['IMG_PATH']); ?>" width="453"
                                               height="567" /></div>
                        <div class="detail-text">
                            <span class="name-text"><?php echo($product['PRO_NAME']); ?></span><br>
                            <?php
                            if ($product['PRO_CODE'] != '') {
                                ?>
                                <span class="name-text"><?php echo($product['PRO_CODE']); ?></span><br>
                                <?php
                            }
                            ?>
                            <span class="price-text"><?php echo(number_format($product['PRO_PRICE'], 1, ',', '.') . " VND"); ?> </span>
                            <br>
                            <span class="detail-header">Tình trạng: </span> <?php echo($statusArray[$product['PRO_STATUS']]); ?>
                            <br>
                            <?php
                            $detailList = getAllDetail();
                            while ($detail = mysql_fetch_array($detailList)) {
                                $detailValue = getDetailValue($proID, $detail['PRD_ID']);
                                if (isset($detailValue) && !empty($detailValue)) {
                                    ?> <span class="detail-header"><?php echo($detail['PRD_NAME']) ?>: </span><?php echo($detailValue['PDV_VALUE']) ?>
                                    <br>
                                    <?php
                                }
                            }
                            ?> <span class="detail-header">Miêu tả: </span> <br>
                            <?php echo($product['PRO_DESCRIPTION']); ?></div>
                        <div class="thumb-block"><a onclick="changeImage(this);"><img style="cursor: pointer"
                                                                                      src="<?php echo($imagePath . $image1['IMG_PATH']); ?>" width="85"
                                                                                      height="106" /></a> <?php
                            if (!empty($image2)) {
                                ?> <a onclick="changeImage(this);"><img id="smallImage" style="cursor: pointer"
                                                                     src="<?php echo($imagePath . $image2['IMG_PATH']); ?>" width="85"
                                                                     height="106" /></a> <?php
                            }
                            ?> <?php
                                if (!empty($image3)) {
                                ?> <a onclick="changeImage(this);"><img style="cursor: pointer"
                                                                     src="<?php echo($imagePath . $image3['IMG_PATH']); ?>" width="85"
                                                                     height="106" /></a> <?php
                            }
                            ?></div>

                    </div>
                    <div class="acc">
                        <div class="paging">Phụ Kiện Đi Kèm</div>
                        <?php
                        $accessories = getRelatedProductByID($proID);
                        while ($accessory = mysql_fetch_array($accessories)) {
                            $mainImage = mysql_fetch_array(getImagesByProID($accessory['PRO_ID']));
                            if ($mainImage['IMG_PATH'] == "") {
                                $mainImagePath = "../img/layout/no-image.png";
                            } else {
                                $mainImagePath = "../img/product/thumbnail/" . $mainImage['IMG_PATH'];
                            }
                            ?>
                            <div class="product-container">
                                <div><a href="detail.php?id=<?php echo($accessory['PRO_ID']); ?>"><img
                                            src="<?php echo($mainImagePath); ?>"
                                            width="178" height="222" /></a></div>
                                <div class="product-text"><a href="#"><?php echo($accessory['PRO_NAME'] . " - " . $accessory['PRO_PRICE'] . " VND") ?></a></div>
                            </div>

                            <?php
                        }
                        ?></div>
                </div>
            </div>
            <?php include("footer.php"); ?></div>
    </body>
</html>